package cn.blmdz.test;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Auther xpoll
 * @Date 2021/4/25
 *
 * https://jsoup.org/
 * <dependency>
 *     <groupId>org.jsoup</groupId>
 *     <artifactId>jsoup</artifactId>
 *     <version>1.13.1</version>
 * </dependency>
 */
public class JsoupTest2 {
    public static JacobTest.MSTTSSpeech speech = new JacobTest.MSTTSSpeech();
    public static String domain = "https://www.xpymw.com/page/";
//     public static boolean c = true;


    public static void main(String[] args) throws Exception {
        List<CCC> cccList = Lists.newArrayList();
//        for (int i = 23; i < 24; i++) {
        for (int i = 1; i < 44; i++) {
            cccList.addAll(goon(domain + i));
        }
        System.out.println(JSON.toJSONString(cccList));
    }
    public static List<CCC> goon(String url) throws Exception {
        Thread.sleep(2000);
        System.out.println(url);
        Document doc = Jsoup.connect(url)
                .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36")
                .get();
//        System.out.println(doc.body().html());
        String body = doc.body().html();
        List<String> stringIdList = Splitter.on("article id=\"post-").splitToList(body);
        List<String> contentIdList = Lists.newArrayList();
        for (int i = 1; i < stringIdList.size(); i++) {
            contentIdList.add("post-" + stringIdList.get(i).split("\"")[0]);
        }
        return contentIdList.stream().map(id -> {
            CCC ccc = new CCC();
            Element content = doc.getElementById(id);
            // 标题
            {
                Elements h2 = content.getElementsByTag("h2");
                Elements a = h2.get(0).getElementsByTag("a");
                String title = a.get(0).text();
                ccc.setTitle(title.trim());
            }
            // 网址
            {
                Elements h2 = content.getElementsByTag("h2");
                Elements a = h2.get(0).getElementsByTag("a");
                String href = a.get(0).attr("href");
                ccc.setHref(href.trim());
            }
            // 类别
            {
                Elements categoryE = content.getElementsByClass("meta-category");
                if (Objects.nonNull(categoryE) && categoryE.size() > 0) {
                    String category = categoryE.get(0).text();
                    ccc.setCategory(category.trim());
                }
            }
            // 金额
            {
                Elements priceE = content.getElementsByClass("meta-price");
                if (Objects.nonNull(priceE) && priceE.size() > 0) {
                    String price = priceE.get(0).text();
                    ccc.setPrice(price.trim());
                }
            }
            // 发布时间
            {
                Elements time = content.getElementsByTag("time");
                if (Objects.nonNull(time) && time.size() > 0) {
                    String datetime = time.get(0).attr("datetime");
                    ccc.setDatetime(datetime.trim());
                }
            }
            // 访问量
            {
                Elements viewsE = content.getElementsByClass("meta-views");
                if (Objects.nonNull(viewsE) && viewsE.size() > 0) {
                    String views = viewsE.get(0).text();
                    ccc.setViews(views.trim());
                }
            }
            if (Objects.isNull(ccc.getTitle())) {
                ccc.setTitle("");

            }
            if (Objects.isNull(ccc.getHref())) {
                ccc.setHref("");
            }
            if (Objects.isNull(ccc.getCategory())) {
                ccc.setCategory("");
            }

            if (Objects.nonNull(ccc.getDatetime())) {
                ccc.setDatetime(ccc.getDatetime().replace("T", " "));
                ccc.setDatetime(ccc.getDatetime().replace("+08:00", ""));
            } else {
                ccc.setDatetime("2000-01-01 00:00:00");
            }
            if (Objects.nonNull(ccc.getPrice())) {
                ccc.setPrice(ccc.getPrice().replace("免费", "0"));
            } else {
                ccc.setPrice("0");
            }
            if (Objects.nonNull(ccc.getViews())) {
                if (ccc.getViews().contains("K")) {
                    ccc.setViews(ccc.getViews().replace("K", ""));
                    ccc.setViews(new BigDecimal(ccc.getViews()).multiply(new BigDecimal(1000)).setScale(0).toString());
                }
            } else {
                ccc.setViews("0");
            }
            return ccc;
        }).collect(Collectors.toList());
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class CCC {
        private String title;
        private String href;
        private String category;
        private String price;
        private String datetime;
        private String views;
    }
}
